<?php include_once '../../essential.php'; ?>
<div id="nav">
	<ul>
		<li><div id="voltar-ico"><a href="#" onclick="openDetail('user', 'index');">Voltar</a></div></li>
	</ul>
</div>
<div id="content">
	<div id="form">
		<?php
			if(isset($_GET['user']))
			{
				edit();
			}else{
				Add();
			}
			
			function edit()
			{
				$user = new User();
				
				$id = $_GET['user'];
				$user->select($id);	
				
				$date = new DateTime($user->get('dt_register'));
				
				$date = date_format($date, "Ymd");
		?>
			<form action='#' class="form-layout" method='post' onSubmit = "return postForm(this, 'user', 'update');";>

				<input type='hidden' name='id' value = '<?php echo $id;?>'/>
				<input type='hidden' name='dt_register' value = '<?php echo $date?>'/>
			
				<div class="display-row">
					<label for="name">Nome</label>
					<input type='text' name='name' id="name" value="<?php echo $user->get('name');?>" class="required"/>
					
					<label for="login">Login</label>
					<input type='text' name='login' id="login" value="<?php echo $user->get('login');?>" class="required"/>
				</div>
					
				<div class="display-row">
					<label for="pass">Senha</label>
					<input type='password' name='pass' maxlength="12" id="re-pass" value="<?php echo $user->get('pass');?>" class="required"/>
				</div>
				
				<div class="display-row">
					<label for="group">Grupo:</label>
					<select name="group" id="group">
						<?php 
							$group = new Group();
						
							$result = $group->listAll();
													
							
							while($grp = mysql_fetch_object($result))
							{
								if($grp->id == $user->get("id_group"))
								{
									echo "<option value='".$grp->id."' selected='selected'>".$grp->name."</option>";	
								}else{
									echo "<option value='".$grp->id."'>".$grp->name."</option>";
								}
							}
						?>
					</select>
				</div>	
				
				<div class="display-row" style="margin-top: 35px; margin-left: 135px;">
					<label>
						<?php
							if($user->get('active')){
								echo "<input type='checkbox' name='active' checked/>";
							}else{
								echo "<input type='checkbox' name='active'/>";
							}
						?>
						Tornar usuário ativo
					</label> 
					<input type='submit' value='Salvar' />
					<?php
						if(isset($_GET['message']))
						{
							echo "<div class='message-status'>".$_GET['message']."</div>";
						}
					?>
					
				</div>
			</form>
			
		<?php
			}
			function Add()
			{
		?>
			<form action='#' class="form-layout" method='post' onSubmit = "return postForm(this, 'user', 'insert');";>
			
				<div class="display-row">
					<label for="name">Nome</label>
					<input type='text' name='name' id="name" class="required"/>
					
					<label for="login">Login</label>
					<input type='text' name='login' id="login" class="required" maxlength="12"/>
				</div>
					
				<div class="display-row">
					<label for="re-pass">Senha</label>
					<input type='password' name='pass' id="pass" class="required" maxlength="12"/>
				</div>
				
				<div class="display-row" style="margin: 0 1px;">
					<label for="group">Grupo:</label>
					<select name="group" id="group">
						<?php 
							$group = new Group();
						
							$result = $group->listAll();
													
							
							while($grp = mysql_fetch_object($result))
							{
								echo "<option value='".$grp->id."'>".$grp->name."</option>";
							}
						?>
					</select>
				</div>		
				
				<div class="display-row" style="margin-top: 35px; margin-left: 135px;">
					<label><input type='checkbox' name='active'/> Tornar usuário ativo</label> 
					<input type='submit' value='Salvar' />
				</div>
				
			</form>

		<?php		
			}
			
			mysql_close();
		?>
	</div>
</div>
<script>
	$(document).ready(function(){
		StartValidation();
	});
</script>
